package com.chat.websocket.controller;

import com.chat.websocket.entity.dto.AddToFriendDTO;
import com.chat.websocket.entity.dto.CheckFriendDTO;
import com.chat.websocket.entity.dto.SearchUserDTO;
import com.chat.websocket.entity.http.Response;
import com.chat.websocket.entity.vo.ChatListVO;
import com.chat.websocket.entity.vo.FriendListVO;
import com.chat.websocket.entity.vo.NewFriendVO;
import com.chat.websocket.service.FriendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * SunJingxuan
 * 2022 \ 08 \ 09
 */
@RestController
@RequestMapping(value = "/friend")
@Slf4j
public class FriendController {

	@Resource
	private FriendService friendService;

	/**
	 * 申请添加好友
	 * @reqstMethod POST
	 * @param addToFriendDTO com.chat.websocket.entity.dto.addToFriendDTO
	 * @return com.chat.websocket.entity.http.Response<String>
	 */
	@PostMapping("/addToFriend")
	public ResponseEntity<Response<String>> addToFriend(@RequestBody AddToFriendDTO addToFriendDTO) {
		try {
			log.info("{}申请添加{}为好友", addToFriendDTO.getUserId(), addToFriendDTO.getFriendUserId());
			friendService.addToFriend(addToFriendDTO);
			return ResponseEntity.ok(Response.success("添加成功", null));
		} catch (Exception e) {
			log.error("添加好友接口出错", e);
			return ResponseEntity.ok(Response.error());
		}
	}

	/**
	 * 查询好友
	 * @reqstMethod GET
	 * @param userId 用户id integer
	 * @return com.chat.websocket.entity.http.Response<com.chat.websocket.entity.vo.FriendListVO>
	 */
	@GetMapping("/searchUserFriend/{userId}")
	public ResponseEntity<Response<List<FriendListVO>>> searchUserFriend(@PathVariable Integer userId) {
		try {
			log.info("查询{}的好友", userId);
			List<FriendListVO> friendLIstVOS = friendService.searchUserFriend(userId);
			return ResponseEntity.ok(Response.success(friendLIstVOS));
		} catch (Exception e) {
			log.error("查询好友接口出错", e);
			return ResponseEntity.ok(Response.error());
		}
	}

	/**
	 * 查询时候有好友申请
	 * @reqstMethod GET
	 * @param userId 用户id integer
	 * @return com.chat.websocket.entity.http.Response<Boolean>
	 */
	@GetMapping("/whetherHaveNewFriend/{userId}")
	public ResponseEntity<Response<Boolean>> whetherHaveNewFriend(@PathVariable Integer userId) {
		try {
			log.info("查询{}是否有好友申请", userId);
			Boolean flag = friendService.whetherHaveNewFriend(userId);
			return ResponseEntity.ok(Response.success(flag));
		} catch (Exception e) {
			log.error("查询是否有好友申请接口出错", e);
			return ResponseEntity.ok(Response.error());
		}
	}

	/**
	 * 查询好友申请
	 * @reqstMethod GET
	 * @param userId 用户id integer
	 * @return com.chat.websocket.entity.http.Response<com.chat.websocket.entity.vo.NewFriendVO>
	 */
	@GetMapping("/searchNewFriend/{userId}")
	public ResponseEntity<Response<List<NewFriendVO>>> searchNewFriend(@PathVariable Integer userId) {
		try {
			log.info("查询{}的好友申请", userId);
			List<NewFriendVO> newFriendVOS = friendService.searchNewFriend(userId);
			return ResponseEntity.ok(Response.success(newFriendVOS));
		} catch (Exception e) {
			log.error("查询好友申请接口出错", e);
			return ResponseEntity.ok(Response.error());
		}
	}

	/**
	 * 拒绝or通过好友申请
	 * @reqstMethod POST
	 * @param checkFriendDTO com.chat.websocket.entity.dto.CheckFriendDTO
	 * @return com.chat.websocket.entity.http.Response<String>
	 */
	@PostMapping("/checkFriend")
	public ResponseEntity<Response<String>> checkFriend(@RequestBody CheckFriendDTO checkFriendDTO) {
		try {
			log.info("拒绝or通过好友申请{}", checkFriendDTO.getVerifyId());
			friendService.checkFriend(checkFriendDTO);
			return ResponseEntity.ok(Response.success(null));
		} catch (Exception e) {
			log.error("拒绝or通过好友申请接口出错", e);
			return ResponseEntity.ok(Response.error());
		}
	}

	/**
	 * 聊天列表
	 * @reqstMethod GET
	 * @param userId 用户id integer
	 * @return com.chat.websocket.entity.http.Response<List<com.chat.websocket.entity.vo.ChatListVO>>
	 */
	@GetMapping("/searchUserChatList/{userId}")
	public ResponseEntity<Response<List<ChatListVO>>> checkFriend(@PathVariable Integer userId) {
		try {
			log.info("{}的聊天列表", userId);
			List<ChatListVO> chatListVOS = friendService.searchUserChatList(userId);
			return ResponseEntity.ok(Response.success(chatListVOS));
		} catch (Exception e) {
			log.error("聊天列表接口出错", e);
			return ResponseEntity.ok(Response.error());
		}
	}

	/**
	 * 改变置顶状态
	 * @reqstMethod GET
	 * @param userId 用户id integer
	 * @param friendUserId 好友用户id integer
	 * @return com.chat.websocket.entity.http.Response<String>
	 */
	@GetMapping("/changeToTopStatus/{userId}/{friendUserId}")
	public ResponseEntity<Response<String>> changeToTopStatus(@PathVariable Integer userId, @PathVariable Integer friendUserId) {
		try {
			log.info("{}改变{}的置顶状态", userId, friendUserId);
			friendService.changeToTopStatus(userId, friendUserId);
			return ResponseEntity.ok(Response.success(null));
		} catch (Exception e) {
			log.error("改变置顶状态接口出错", e);
			return ResponseEntity.ok(Response.error());
		}
	}

	/**
	 * 聊天列表的状态
	 * @reqstMethod GET
	 * @param userId 用户id integer
	 * @param friendUserId 好友用户id integer
	 * @return com.chat.websocket.entity.http.Response<String>
	 */
	@GetMapping("/changeChatListDeleteStatus/{userId}/{friendUserId}")
	public ResponseEntity<Response<String>> changeChatListDeleteStatus(@PathVariable Integer userId, @PathVariable Integer friendUserId) {
		try {
			log.info("{}改变{}的聊天列表的删除状态", userId, friendUserId);
			friendService.changeChatListDeleteStatus(userId, friendUserId);
			return ResponseEntity.ok(Response.success(null));
		} catch (Exception e) {
			log.error("聊天列表的删除状态接口出错", e);
			return ResponseEntity.ok(Response.error());
		}
	}

	/**
	 * 查询已添加的好友
	 * @reqstMethod POST
	 * @param searchUserDTO com.chat.websocket.entity.dto.SearchUserDTO
	 * @return com.chat.websocket.entity.http.Response<List<com.chat.websocket.entity.vo.ChatListVO>>
	 */
	@PostMapping("/searchFriend")
	public ResponseEntity<Response<List<ChatListVO>>> searchFriend(@RequestBody SearchUserDTO searchUserDTO) {
		try {
			log.info("查询已添加好友{}", searchUserDTO);
			List<ChatListVO> chatListVOS = friendService.searchFriend(searchUserDTO);
			return ResponseEntity.ok(Response.success(chatListVOS));
		} catch (Exception e) {
			log.error("查询已添加好友接口出错", e);
			return ResponseEntity.ok(Response.error());
		}
	}
}
